<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form
          ref="userForm"
          label-width="120px"
          style="margin-left: 120px; margin-top: 30px"
          :model="form"
          :rules="rules"
        >
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="form.username" style="width: 300px" />
          </el-form-item>
          <el-form-item label="手机">
            <el-input v-model="form.mobile" style="width: 300px" disabled />
          </el-form-item>
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="form.timeOfEntry"
              type="date"
              class="inputW"
            />
          </el-form-item>
          <el-form-item label="员工头像" />
          <ImageUpload />
          <el-form-item>
            <el-button type="primary" @click="ModifyEmployeeInformation">更新</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { PersonalDetailsAPI, ModifyEmployeeInformationAPI } from '@/api/user'
export default {
  name: 'EmployeesDetail',
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        username: [{ required: true, message: '角色名称不可为空', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '入职时间不可为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.PersonalDetails()
  },
  methods: {
    // 数据回显
    async PersonalDetails() {
      // 获取到父组件传来的数据
      const userId = this.$route.params.id
      // 获取到员工信息
      const { data } = await PersonalDetailsAPI(userId)
      // 将数据传给form显现在页面
      // console.log(res)
      this.form = data
    },
    // 点击修改数据
    ModifyEmployeeInformation() {
      this.$refs.userForm.validate(async vali => {
        // 判断是否校验通过
        if (!vali) return
        // 通过就上传数据
        const res = await ModifyEmployeeInformationAPI(this.form)
        // 打印提示
        this.$message.success(res.message)
      })
    }
  }
}
</script>
